package com.sgb.link.api;

import android.accounts.NetworkErrorException;
import android.text.TextUtils;


import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.helper.AppManagerHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.NetWorkUtil;

import org.jetbrains.annotations.NotNull;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 王彬臣
 * @date: 2021/9/28
 * @Description: 网络请求回调统一处理部分事件 可控制是否toast提示
 *
 */
public abstract class CommonCallback<T> implements Callback<T> {
    //是否需要toast提示
    private final boolean mIsNeedErrorToast;

    public CommonCallback() {
        this(true);
    }

    public CommonCallback(boolean needErrorToast) {
        mIsNeedErrorToast = needErrorToast;
    }

    @Override
    public void onResponse(@NotNull Call<T> call, @NotNull Response<T> response) {
        DialogHelper.dismissProgressDialog();
        //todo 统一处理部分事件
        if (response.body() instanceof BaseEntity) {
            BaseEntity entity = (BaseEntity) response.body();
            //错误提示
            if (!entity.isSuccess()) {
                if (mIsNeedErrorToast && !TextUtils.isEmpty(entity.getMsg())) {
                    MToast.showToast(AppManagerHelper.getAppManagerHelper().currentActivity(), String.format("%s", entity.getMsg()));
                }
                failure(new CommonThrowable(entity));
                onFailureWithEntity(entity);
                return;
            }
        }
        if (response.body() == null) {
            failure(new Throwable("response body is null"));
            return;
        }
        response(response.body());
    }
    /**
     * 失败带返回体数据
     */
    public void onFailureWithEntity(BaseEntity entity){

    }
    protected abstract void response(T data);

    @Override
    public void onFailure(@NotNull Call<T> call, @NotNull Throwable t) {
        DialogHelper.dismissProgressDialog();
        t.printStackTrace();
        BaseEntity entity=new BaseEntity();
        boolean isNetWork = NetWorkUtil.isNetworkConnected(AppManagerHelper.getAppManagerHelper().currentActivity());
        if (!isNetWork) {
            entity.setMsg("无网络，请检查网络连接");
            if(mIsNeedErrorToast) {
                MToast.showToast(AppManagerHelper.getAppManagerHelper().currentActivity(), "无网络，请检查网络连接");
            }
        } else {
            if (t instanceof ConnectException
                    || t instanceof UnknownHostException) {
                entity.setMsg("网络链接不稳定，请稍后重试");
                if(mIsNeedErrorToast) {
                    MToast.showToast(AppManagerHelper.getAppManagerHelper().currentActivity(), "网络链接不稳定，请稍后重试");
                }
            } else if (t instanceof NetworkErrorException) {
                entity.setMsg("无网络，请检查网络连接");
                if(mIsNeedErrorToast) {
                    MToast.showToast(AppManagerHelper.getAppManagerHelper().currentActivity(), "无网络，请检查网络连接");
                }
            } else if (t instanceof SocketTimeoutException) {
                entity.setMsg("网络连接超时，请稍后再试");
                if(mIsNeedErrorToast) {
                    MToast.showToast(AppManagerHelper.getAppManagerHelper().currentActivity(), "网络连接超时，请稍后再试");
                }
            }
        }
        //todo 统一处理失败事件
        failure(new CommonThrowable(entity));
    }

    protected abstract void failure(Throwable t);
}
